<?php
require_once("includes/common.php");
$msg = '';

if(isset($_SESSION['id'])) {
    header('location:my_profile.php');
}

if( !empty($_POST))
{
if(trim($_POST['email']) != '' && trim($_POST['password'])!= '') {
  ////////////////COOKIE /////////////////////////
  if(isset($_POST['remember'])) {
    setcookie('email',trim($_POST['email']),time()+60*60*1);
    setcookie('password',trim($_POST['password']),time()+60*60*1);
  } else {
    setcookie('email',trim($_POST['email']),time()-1);
    setcookie('password',trim($_POST['password']),time()-1);
  }
  /////////////////////////////////////////
  $email = mysql_real_escape_string($_POST['email']);
  $password = mysql_real_escape_string($_POST['password']);
  $password = md5($password);

  echo $loginQuery = "SELECT * FROM `users` WHERE `email`='$email' AND `password`='$password' AND `status`='1'";
  $result = mysql_query($loginQuery) or die(mysql_error());
  if(mysql_num_rows($result) == 1) {
    $userData = mysql_fetch_assoc($result);
    $_SESSION['id'] = $userData['id'];
    $_SESSION['name'] = $userData['name'];
    $_SESSION['email'] = $userData['email'];
    header('location:my_profile.php');
  } else {
    $msg = "<span style=\"color:red\">Invalid Login Details</span>";
    }
  } else {
    $msg = '<span style="color:red">Email/password should not be blank</span>';
  }
}

$chk = isset($_COOKIE['email']) ? 'checked' :'';
?>
<div >
<h3 style="text-align: center;">Login Here</h3>

<!--- Login message-->
<div><?=$msg?> </div>
   <link href="css/bootstrap.css" type="text/css" rel="stylesheet" />
   <link href="css/site.css" type="text/css" rel="stylesheet" />

<form class="form-horizontal" method="post">
  <div class="control-group">
    <label class="control-label" for="inputEmail">Email</label>
    <div class="controls">
      <input type="text" id="inputEmail" placeholder="Email" name="email" value="<?=@$_COOKIE['email']?>">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">Password</label>
    <div class="controls">
      <input type="password" id="inputPassword" placeholder="Password" name="password" value="<?=@$_COOKIE['password']?>">
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
      <label class="checkbox">
        <input type="checkbox" <?=$chk?> name="remember"value="1"> Remember me
      </label>
      <button type="submit" class="btn btn-primary btn-large">Sign in</button>
      <a href="registration.php">Register</a>
    </div>
  </div>
</form>
<div>
